// pages/add/add.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    show: false,
    checked: 1,
    type: '游戏',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var type = options.type
    var checked = options.checked

    var date = new Date();
    date.setTime(date.getTime());
    var s2 = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
    this.setData({
      date: s2,
      type,
      checked
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.setData({
      show: false,
      date: this.formatDate(event.detail),
    });
    console.log(this.data.date)
  },
  formSubmit(e) {
    var billDate = this.data.date
    var billType = "支出"
    var money = e.detail.value.money
    var remarks = e.detail.value.remarks
    var type = this.data.type
    console.log(type)
    if (this.data.checked == 1) {
      billType = "支出"
    } else {
      billType = "收入"
    }

    var userId = getApp().globalData.userId

    var bill = {
      billId: 1,
      userId: userId,
      billDate,
      billType,
      type,
      money,
      remarks
    }
    console.log(bill)
    wx.request({
      url: 'http://127.0.0.1:8181/bill/addBill',
      data: bill,
      success(res) {
        console.log(res)
        wx.navigateBack({
          delta: 1,
        })
      }
    })
  }
})